package client_gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import actions.RegisterAction;
import utils.Constants;
import utils.ImageCreator;

/**
 * Klasa predstavlja dialog za registraciju korisnika.
 * Klikom na dugme register, serveru se salju parametri za registraciju korisnika.
 * 
 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
 */
public class RegisterDialog extends JDialog implements Constants {

	
	/**
	 * Parent - prozor aplikacije.
	 */
	private ClientWindow owner;
	
	
	/**
	 * ID avatara koji se koristi pri registraciji.
	 */
	private String avatarID = "avatar01";
	
	
	private JPanel jContentPane = null;
	
	
	
	/**
	 * LayoutManager.
	 */
	private GridBagLayout gridBag = null;
	
	
	// ogranicenja komponenti u GridBagLayout manager-u
	private GridBagConstraints usernameLabelConstraint = null;
	private GridBagConstraints usernameFieldConstraint = null;
	private GridBagConstraints passwordLabelConstratin = null;
	private GridBagConstraints passwordFieldConstraint = null;
	private GridBagConstraints confirmLabelConstraint = null;
	private GridBagConstraints confirmFieldConstraint = null;
	private GridBagConstraints nicknameLabelConstraint = null;
	private GridBagConstraints nicknameFieldConstraint = null;
	private GridBagConstraints emailLabelConstraint = null;
	private GridBagConstraints emailFieldConstraint = null;
	private GridBagConstraints avatarLabelConstraint = null;
	private GridBagConstraints avatarButtonConstraint = null;
	private GridBagConstraints opcioniUnosConstraint = null;
	
	
	/**
	 * Panel za registraciju.
	 */
	private JPanel registringPanel = null;
	
	
	/**
	 * Panel sa dugmadima.
	 */
	private JPanel buttonPanel = null;
	
	
	// komponente RegisterDialog-a
	private JLabel usernameLabel = null;
	private JTextField usernameField = null;
	private JLabel passwordLabel = null;
	private JPasswordField passwordField = null;
	private JLabel confirmLabel = null;
	private JPasswordField confirmField = null;
	private JLabel nicknameLabel = null;
	private JTextField nicknameField = null;
	private JLabel emailLabel = null;
	private JTextField emaliField = null;
	private JLabel avatarLabel = null;
	private JButton avatarButton = null;
	private RegisterAction registerButton = null;
	private JButton cancelButton = null;
	private JLabel opcioniUnosLabel = null;

	
	/**
	 * Dimenzije field komponenti (Text i Password)
	 */
	private Dimension fieldDimension = new Dimension(150,20);
	

	/**
	 * Konstruktor.
	 * @param parent - prozor aplikacije
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public RegisterDialog(ClientWindow parent) {
		super(parent, "Register dialog", true);
		
		this.owner = parent;
		
		this.gridBag = new GridBagLayout();
		
		usernameLabelConstraint = new GridBagConstraints();
		usernameFieldConstraint = new GridBagConstraints();
		passwordLabelConstratin = new GridBagConstraints();
		passwordFieldConstraint = new GridBagConstraints();
		confirmLabelConstraint = new GridBagConstraints();
		confirmFieldConstraint = new GridBagConstraints();
		nicknameLabelConstraint = new GridBagConstraints();
		nicknameFieldConstraint = new GridBagConstraints();
		emailLabelConstraint = new GridBagConstraints();
		emailFieldConstraint = new GridBagConstraints();
		avatarLabelConstraint = new GridBagConstraints();
		avatarButtonConstraint = new GridBagConstraints();
		opcioniUnosConstraint = new GridBagConstraints();
		
		initializeConstraints();
		
		initialize();
	}
	
	
	
	/**
	 * Metod vrsi inicijalizaciju dialoga.
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	private void initialize() {
		this.setSize(300, 350);
		this.setLocation(owner.getX() + owner.getWidth() / 2 - this.getWidth() / 2, owner.getY() + owner.getHeight() / 2 - this.getHeight() / 2);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		
		this.setIconImage(owner.getIconImage());
	}
	
	
	/**
	 * Metod vrsi inicijalizaciju ogranicenja komponenti u GridBagLayout manager-u.
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	private void initializeConstraints() {
		usernameLabelConstraint.gridx = 0;
		usernameLabelConstraint.gridy = 0;
		usernameLabelConstraint.weightx = 1;
		usernameLabelConstraint.weighty = 1;
		usernameLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		usernameLabelConstraint.insets = new Insets(5,10,2,0);
		
		usernameFieldConstraint.gridx = 1;
		usernameFieldConstraint.gridy = 0;
		usernameFieldConstraint.weightx = 1;
		usernameFieldConstraint.weighty = 1;
		usernameFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		usernameFieldConstraint.insets = new Insets(5,10,2,0);
		
		passwordLabelConstratin.gridx = 0;
		passwordLabelConstratin.gridy = 1;
		passwordLabelConstratin.weightx = 1;
		passwordLabelConstratin.weighty = 1;
		passwordLabelConstratin.anchor = GridBagConstraints.NORTHWEST;
		passwordLabelConstratin.insets = new Insets(0,10,2,0);
		
		passwordFieldConstraint.gridx = 1;
		passwordFieldConstraint.gridy = 1;
		passwordFieldConstraint.weightx = 1;
		passwordFieldConstraint.weighty = 1;
		passwordFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		passwordFieldConstraint.insets = new Insets(0,10,2,0);
		
		confirmLabelConstraint.gridx = 0;
		confirmLabelConstraint.gridy = 2;
		confirmLabelConstraint.weightx = 1;
		confirmLabelConstraint.weighty = 1;
		confirmLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		confirmLabelConstraint.insets = new Insets(0,10,2,0);
		
		confirmFieldConstraint.gridx = 1;
		confirmFieldConstraint.gridy = 2;
		confirmFieldConstraint.weightx = 1;
		confirmFieldConstraint.weighty = 1;
		confirmFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		confirmFieldConstraint.insets = new Insets(0,10,2,0);
		
		opcioniUnosConstraint.gridx = 0;
		opcioniUnosConstraint.gridy = 3;
		opcioniUnosConstraint.weightx = 1;
		opcioniUnosConstraint.weighty = 1;
		opcioniUnosConstraint.anchor = GridBagConstraints.NORTHWEST;
		opcioniUnosConstraint.insets = new Insets(30,10,2,0);
				
		nicknameLabelConstraint.gridx = 0;
		nicknameLabelConstraint.gridy = 4;
		nicknameLabelConstraint.weightx = 1;
		nicknameLabelConstraint.weighty = 1;
		nicknameLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameLabelConstraint.insets = new Insets(10,10,2,0);
		
		nicknameFieldConstraint.gridx = 1;
		nicknameFieldConstraint.gridy = 4;
		nicknameFieldConstraint.weightx = 1;
		nicknameFieldConstraint.weighty = 1;
		nicknameFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameFieldConstraint.insets = new Insets(10,10,2,0);
		
		emailLabelConstraint.gridx = 0;
		emailLabelConstraint.gridy = 5;
		emailLabelConstraint.weightx = 1;
		emailLabelConstraint.weighty = 1;
		emailLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		emailLabelConstraint.insets = new Insets(0,10,2,0);
		
		emailFieldConstraint.gridx = 1;
		emailFieldConstraint.gridy = 5;
		emailFieldConstraint.weightx = 1;
		emailFieldConstraint.weighty = 1;
		emailFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		emailFieldConstraint.insets = new Insets(0,10,2,0);
		
		avatarLabelConstraint.gridx = 0;
		avatarLabelConstraint.gridy = 6;
		avatarLabelConstraint.weightx = 1;
		avatarLabelConstraint.weighty = 1;
		avatarLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		avatarLabelConstraint.insets = new Insets(0,10,2,0);
		
		avatarButtonConstraint.gridx = 1;
		avatarButtonConstraint.gridy = 6;
		avatarButtonConstraint.weightx = 1;
		avatarButtonConstraint.weighty = 1;
		avatarButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		avatarButtonConstraint.insets = new Insets(0,10,2,0);
	}
	
	
	
	
	// Metode koje inicijalizuju komponente dialoga.
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			
			jContentPane.setLayout(new BorderLayout());
			
			jContentPane.add(getRegisteringPanel(), BorderLayout.CENTER);
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
		}
		
		return jContentPane;
	}
	
	
	private JPanel getRegisteringPanel() {
		if (registringPanel == null) {
			registringPanel = new JPanel();
			
			registringPanel.setLayout(gridBag);
			
			TitledBorder titledBorder = BorderFactory.createTitledBorder(BorderFactory.
					createEtchedBorder(EtchedBorder.RAISED),
					"Registering information", javax.swing.border.TitledBorder.CENTER,
					javax.swing.border.TitledBorder.DEFAULT_POSITION,
					null, null);
			
			registringPanel.setBorder(titledBorder);
			
			registringPanel.add(getUsernameLabel());
			registringPanel.add(getUsernameField());
			registringPanel.add(getPasswordLabel());
			registringPanel.add(getPasswordField());
			registringPanel.add(getConfirmLabel());
			registringPanel.add(getConfirmFiled());
			registringPanel.add(getNicknameLabel());
			registringPanel.add(getNicknameField());
			registringPanel.add(getEmailLabel());
			registringPanel.add(getEmailField());
			registringPanel.add(getAvatarLabel());
			registringPanel.add(getAvatarButton());
			registringPanel.add(getOpcioniUnosLabel());
			
		}
		
		return registringPanel;
	}
	
	
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			FlowLayout bpFlow = new FlowLayout(FlowLayout.CENTER);
			bpFlow.setHgap(20);
			buttonPanel.setLayout(bpFlow);
			
			buttonPanel.add(getRegisterButton());
			buttonPanel.add(getCancelButton());
		}
		
		return buttonPanel;
	}
	
	
	
	private RegisterAction getRegisterButton() {
		if (registerButton == null) {
			registerButton = new RegisterAction(owner, RegisterDialog.this, "Register");
			registerButton.setPreferredSize(new Dimension(100,24));
		}
		
		return registerButton;
	}
	
	
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton("Cancel");
			cancelButton.setPreferredSize(new Dimension(100,24));
			
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					RegisterDialog.this.dispose();
				}
			});
		}
		
		return cancelButton;
	}
	
	
	private JLabel getUsernameLabel() {
		if (usernameLabel == null) {
			usernameLabel = new JLabel("Username:");
			gridBag.setConstraints(usernameLabel, usernameLabelConstraint);
		}
		
		return usernameLabel;
	}
	
	
	private JTextField getUsernameField() {
		if (usernameField == null) {
			usernameField = new JTextField();
			usernameField.setPreferredSize(fieldDimension);
			gridBag.setConstraints(usernameField, usernameFieldConstraint);
		}
		
		return usernameField;
	}
	
	
	private JLabel getPasswordLabel() {
		if (passwordLabel == null) {
			passwordLabel = new JLabel("Password:");
			gridBag.setConstraints(passwordLabel, passwordLabelConstratin);
		}
		
		return passwordLabel;
	}
	
	
	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setPreferredSize(fieldDimension);
			gridBag.setConstraints(passwordField, passwordFieldConstraint);
		}
		
		return passwordField;
	}
	
	
	private JLabel getConfirmLabel() {
		if (confirmLabel == null) {
			confirmLabel = new JLabel("Confirm pass:");
			gridBag.setConstraints(confirmLabel, confirmLabelConstraint);
		}
		
		return confirmLabel;
	}
	
	
	private JPasswordField getConfirmFiled() {
		if (confirmField == null) {
			confirmField = new JPasswordField();
			confirmField.setPreferredSize(fieldDimension);
			gridBag.setConstraints(confirmField, confirmFieldConstraint);
		}
		
		return confirmField;
	}
	
	
	private JLabel getNicknameLabel() {
		if (nicknameLabel == null) {
			nicknameLabel = new JLabel("Nickname:");
			gridBag.setConstraints(nicknameLabel, nicknameLabelConstraint);
		}
		
		return nicknameLabel;
	}
	
	
	private JTextField getNicknameField() {
		if (nicknameField == null) {
			nicknameField = new JTextField();
			nicknameField.setPreferredSize(fieldDimension);
			nicknameField.setText("NewUser");
			gridBag.setConstraints(nicknameField, nicknameFieldConstraint);
		}
		
		return nicknameField;
	}
	
	
	private JLabel getEmailLabel() {
		if (emailLabel == null) {
			emailLabel = new JLabel("E-mail:");
			gridBag.setConstraints(emailLabel, emailLabelConstraint);
		}
		
		return emailLabel;
	}
	
	
	private JTextField getEmailField() {
		if (emaliField == null) {
			emaliField = new JTextField();
			emaliField.setPreferredSize(fieldDimension);
			gridBag.setConstraints(emaliField, emailFieldConstraint);
		}
		
		return emaliField;
	}
	
	
	private JLabel getAvatarLabel() {
		if (avatarLabel == null) {
			avatarLabel = new JLabel("Avatar:");
			gridBag.setConstraints(avatarLabel, avatarLabelConstraint);
		}
		
		return avatarLabel;
	}
	
	
	private JButton getAvatarButton() {
		if (avatarButton == null) {
			avatarButton = new JButton();
			avatarButton.setIcon(ImageCreator.createImageIcon(RegisterDialog.class, DEFAULT_AVATAR));
			avatarButton.setPreferredSize(new Dimension(50,50));
			gridBag.setConstraints(avatarButton, avatarButtonConstraint);
			
			avatarButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					AvatarDialog avatarDialog = new AvatarDialog(RegisterDialog.this);
					avatarDialog.setVisible(true);
				}
			});
		}
		
		return avatarButton;
	}
	
	
	private JLabel getOpcioniUnosLabel() {
		if (opcioniUnosLabel == null) {
			opcioniUnosLabel = new JLabel("Optional");
			
			Font font = new Font("Arial", Font.PLAIN, 14);
			opcioniUnosLabel.setFont(font);
			
			gridBag.setConstraints(opcioniUnosLabel, opcioniUnosConstraint);
		}
		
		return opcioniUnosLabel;
	}
	

	
	/**
	 * Metod setuje ID avatara na avatarID, pri izboru avatara iz AvatarDialog-a.
	 * Istevremeno se i setuje ikonica avatar dugmeta.
	 * @param avatarID - novi ID avatara
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public void setAvatar(String avatarID) {
		this.avatarID = avatarID;
		avatarButton.setIcon(ImageCreator.createImageIcon(RegisterDialog.class, "/avatars/" + avatarID + ".png"));
	}
	
	
	
	/**
	 * Vraca vlasnika ovog dijaloga.
	 * @return - vlasnik dijaloga
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public ClientWindow getRegisterOwner() {
		return this.owner;
	}
	
	
	
	/**
	 * Metod vraca username.
	 * @return - username
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getUsernameFieldEntry() {
		return this.usernameField.getText();
	}
	
	
	/**
	 * Metod vraca password.
	 * @return - password
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getPasswordFieldEntry() {
		return new String(this.passwordField.getPassword());
	}
	
	
	/**
	 * Metod vraca potvrdjeni password.
	 * @return - potvrdjeni password
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getConfirmPasswordFieldEntry() {
		return new String(this.confirmField.getPassword());
	}

	
	/**
	 * Metod vraca nickname.
	 * @return - nickname
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getNicknameFieldEntry() {
		return this.nicknameField.getText();
	}
	
	
	/**
	 * Metod vraca email.
	 * @return - email
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getEmailFieldEntry() {
		return this.emaliField.getText();
	}
	
	
	/**
	 * Metod vraca ID avatara.
	 * @return - ID avatara
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public String getAvatarID() {
		return this.avatarID;
	}
	
}
